package com.ybg.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

 /**
 * @author huang nc
 * @since  2017年4月18日
 */
public class CSVUtils {
		/**
	   * 生成为CVS文件 
	   * @param exportData 源数据List
	   * @param map        csv文件的列表头map
	   * @param outPutPath 文件路径
	   * @param fileName    文件名称
	   * @return
	   */
	  @SuppressWarnings("rawtypes")  
	  public static File createCSVFile(List<Map<String, Object>> exportData, LinkedHashMap<String, Object> map, String outPutPath,String fileName) {  
	    File csvFile = null;  
	    BufferedWriter csvFileOutputStream = null;  
	    try {  
	      File file = new File(outPutPath);  
	      if (!file.exists()) {  
	        file.mkdir();  
	      }  
	      //定义文件名格式并创建  
	      //csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath)); 
	      csvFile = new File(outPutPath, fileName);
	      System.out.println("csvFile：" + csvFile);  
	      // UTF-8使正确读取分隔符","
	      csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024); 
	      System.out.println("csvFileOutputStream：错了吗？" + csvFileOutputStream);  
	      // 写入文件头部  
	      for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {  
	        Entry<String, Object> propertyEntry = (Entry<String,Object>) propertyIterator.next(); 
	        csvFileOutputStream.write(new String(new byte[] {(byte) 0xEF,(byte) 0xBB,(byte) 0xBF }));
	        csvFileOutputStream.write("" + (String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "" + "");  
	        if (propertyIterator.hasNext()) {  
	          csvFileOutputStream.write(",");  
	        }  
	      }  
	      csvFileOutputStream.newLine();  
	      // 写入文件内容  
	      for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {  
	        Object row = (Object) iterator.next();  
	        for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {  
	          java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();  
	          String content = (String) BeanUtils.getProperty(row,(String) propertyEntry.getValue());
	          csvFileOutputStream.write(new String(new byte[] {(byte) 0xEF,(byte) 0xBB,(byte) 0xBF }));
	          csvFileOutputStream.write(content+"");  
	          if (propertyIterator.hasNext()) {  
	            csvFileOutputStream.write(",");  
	          }  
	        }  
	        if (iterator.hasNext()) {  
	          csvFileOutputStream.newLine(); 
	        }  
	      }  
	      csvFileOutputStream.flush();  
	    } catch (Exception e) {  
	      e.printStackTrace();  
	    } finally {  
	      try {  
	        csvFileOutputStream.close();  
	      } catch (IOException e) {  
	        e.printStackTrace();  
	      }  
	    }  
	    return csvFile;  
	  }  
	  
}
